package com.gcph.zhuoba.player.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.gcph.zhuoba.R;
import com.gcph.zhuoba.main.common.BaseActivity;
import com.gcph.zhuoba.player.client.PlayerPracticeClient;
import com.gcph.zhuoba.player.client.PlayerPreviewClient;
import com.gcph.zhuoba.util.L;

/**
 * 干货的开始
 * @author zhanghejie
 */
public class GanhuoBeginActivity extends BaseActivity {
	//退出
    private Button btn_ganhuo_quit;
    private Button btn_ganhuo_rebegin;
    private ImageView ganhuo_desk_img;
    
    private static Handler socketHandler;
    private PlayerPreviewClient socketClient;
    private String data;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.fullScreen(true);
		//屏蔽home键
		this.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);
		setContentView(R.layout.activity_ganhuo_begin);
		Intent intent = this.getIntent(); 
		data=intent.getStringExtra("data");
		int id = getDrawableIdFromData(data);
		if(0!=id)
		{
			ganhuo_desk_img = (ImageView) this.findViewById(R.id.ganhuo_desk_img);
			ganhuo_desk_img.setImageResource(id); 
		}
		
		socketClient = PlayerPreviewClient.getInstance();
		socketHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
            	//配对失败
            	if(PlayerPracticeClient.PAIR_FAIL == msg.what)
            	{
            		Toast.makeText(GanhuoBeginActivity.this, "与终端配对失败", Toast.LENGTH_SHORT).show();
            		finish();
            	}
            	//配对成功
            	if(PlayerPracticeClient.PAIR_SUCC == msg.what)
            	{
            		//发送开货开始指令
            		socketClient.send("{\"method\":\"802\",\"data\":\""+data+"\"}");
            	}
            }
		};
		
		
		//----------------------配对开始----------------------------
		new Thread(new Runnable() {
			@Override
			public void run() {
				String socket_ip = getResources().getString(R.string.socket_ip);
				int socket_port = Integer.parseInt(getResources().getString(R.string.socket_port));
				socketClient.setUpConnection(socket_ip,socket_port,socketHandler);
				L.d("======请求配对=====");
				socketClient.send("{\"method\":\"102\",\"deviceid\":\"1\",\"username\":\"test\"}");
				socketClient.handleReceive(socketHandler);
			}
		}).start();
		//----------------------配对结束----------------------------
		
		
		//重新开始
		btn_ganhuo_rebegin = (Button) this.findViewById(R.id.btn_ganhuo_rebegin);
		btn_ganhuo_rebegin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
            	socketClient.send("{\"method\":\"803\",\"data\":\""+data+"\"}");
            }
        });
		
		//退出预览
		btn_ganhuo_quit = (Button) this.findViewById(R.id.btn_ganhuo_quit);
		btn_ganhuo_quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
            	//退出断开连接
            	socketClient.exit();
            	finish();
            }
        });
		
	}
	
	/**
	 * 
	 * @param data
	 * @return
	 */
	private int getDrawableIdFromData(String data)
	{
		if("1-1".equals(data))
		{
			return R.drawable.ganhuo1_desk_1;
		}else if("1-2".equals(data))
		{
			return R.drawable.ganhuo1_desk_2;
		}else if("1-3".equals(data))
		{
			return R.drawable.ganhuo1_desk_3;
		}else if("2-1".equals(data))
		{
			return R.drawable.ganhuo2_desk_1;
		}else if("2-2".equals(data))
		{
			return R.drawable.ganhuo2_desk_2;
		}else if("2-3".equals(data))
		{
			return R.drawable.ganhuo2_desk_3;
		}else if("2-4".equals(data))
		{
			return R.drawable.ganhuo2_desk_4;
		}
			
		return 0;
	}
	@Override
	protected void onResume() {
		super.onResume();
	}
	//===========================禁用home键，返回键和菜单键====================================
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		} else if (keyCode == KeyEvent.KEYCODE_MENU) {
			return false;
		} else if (keyCode == KeyEvent.KEYCODE_HOME) {
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}